
#include "audio_recorder.h"
#include <string>
#include <Windows.h>

using namespace std;

static char *dup_wchar_to_utf8(wchar_t *w)
{
    char *s = NULL;
    int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
    s = (char *)av_malloc(l);
    if (s)
        WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
    return s;
}

int main(){
    puts("==== Audio Recorder ====");
    avdevice_register_all();

    wchar_t w[200] = L"audio=麦克风阵列 (Realtek Audio)";
    std::string deviceName = dup_wchar_to_utf8(w);

    AudioRecorder recorder{ "test_audio.aac", deviceName};
    try {
        recorder.open();
        recorder.start();

        //record 10 seconds.
        std::this_thread::sleep_for(10s);

        recorder.stop();
        string reason = recorder.failReason();
        if (!reason.empty()) {
            throw std::runtime_error(reason);
        }
    }
    catch (std::exception &e) {
        fprintf(stderr,"[ERROR] %s\n", e.what());
        exit(-1);
    }

    puts("END");
    return 0;
}